package com.blue.utils;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.blue.example.ProgressFrom;
import com.blue.pojo.WebApp;
import com.blue.pojo.WebConfigVo;
import javafx.concurrent.Task;
import javafx.scene.control.Label;
import javafx.stage.Stage;
import sun.misc.BASE64Encoder;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.util.ArrayList;

/**
 * Created by Administrator on 2019/7/4.
 */
public class TomcatHTMLUtil {
    private static int i = 0;
    private String tomcatpath;
    private String tomcatPort;
    private int count;
    private String webFront;
    private String webData;
    private ArrayList<String> webList;
    private WebConfigVo webConfigVo;
    public ArrayList<WebApp> webAppArrayList = new ArrayList<WebApp>();
    public boolean updatePro = false;
    public boolean reloadWebFront = false;
    public boolean updateSuccess = false;
    public int n;

    public TomcatHTMLUtil() {
    }

    public TomcatHTMLUtil(String tomcatpath, String tomcatport, int count) {
        this.tomcatpath = tomcatpath;
        this.tomcatPort = tomcatport;
        this.count = count;
    }

    public TomcatHTMLUtil(String tomcatpath, String tomcatport, String webFront,
                          String webData, ArrayList<String> webList, int n, WebConfigVo webConfigVo) {
        this.tomcatpath = tomcatpath;
        this.tomcatPort = tomcatport;
        this.count = count;
        this.webFront = webFront;
        this.webData = webData;
        this.webList = webList;
        this.n = n;
        this.webConfigVo = webConfigVo;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getN() {
        return n;
    }

    public void setN(int n) {
        this.n = n;
    }

    public static void main(String[] args) {
        /*boolean startWebApp = startWebApp( "mybatisboot-0.0.1" );
        System.out.println( startWebApp );*/
       /* ArrayList<WebApp> tomcatWebAppData = getTomcatWebAppData( "8083", "mybatisboot-0.0.1" );
        for (WebApp web:tomcatWebAppData
             ) {
            if ("运行".equals( web.getStatus() )){
                System.out.print( web.getName() );
            }
        }*/
        boolean b = reloadWebApp( "mybatisboot-0.0.1", "8083" );
        System.out.print( b );

    }

    public static String message(String operateURL) {

        StringBuffer dataResult = new StringBuffer();
        URL url = null;
        try {
            i++;
            url = new URL( operateURL );

            URLConnection conn = (URLConnection) url.openConnection();
            //这里我是把admin，123456 这个用户信息放到了一个json文件中以json形式存放，然后取出来，如果不是以这种方式存放，则可以直接设置username = admin ，password =123456
            /*String data = FileUtil.readJson();

            String username = data.substring( 1, data.length() - 1 ).split( "," )[0].split( ":" )[1].substring
                    ( 1, data.substring( 1, data.length() - 1 ).split( "," )[0].split( ":" )[1].length() - 1 );
            String password = data.substring( 1, data.length() - 1 ).split( "," )[1].split( ":" )[1].substring
                    ( 1, data.substring( 1, data.length() - 1 ).split( "," )[0].split( ":" )[1].length() );*/
            String username = "admin";
            String password = "abc";
            String configuration = username + ":" + password; // manager角色的用户
            String encodedPassword = new BASE64Encoder().encode( configuration.getBytes() );
            conn.setRequestProperty( "Authorization", "Basic " + encodedPassword );
            // URL授权访问 -- End

            InputStream is = conn.getInputStream();
            BufferedReader bufreader = new BufferedReader( new InputStreamReader( is ) );
            String line = null;
            while ((line = bufreader.readLine()) != null) {
                dataResult.append( line );
                System.out.println( "project reload status:" + dataResult.toString() );
            }
        } catch (Exception e) {
            e.printStackTrace();
            message( operateURL );
            if (5 == i) {
                return null;
            }
        }
        return dataResult.toString();
    }


    /**
     * 获取tomcat正在运行的webApp的参数
     *
     * @return
     */
    public static ArrayList<WebApp> getTomcatWebAppData(String port, String webName) {

        ArrayList<WebApp> webAppArrayList = new ArrayList<WebApp>();

        String data = TomcatHTMLUtil.message( "http://localhost:" + port + "/manager/text/list" );

        String[] oldDataStrs = data.split( "/" );

        if (oldDataStrs[0].startsWith( "OK" )) {
            for (int i = 0; i < oldDataStrs.length; i++) {
                String name = oldDataStrs[i].split( ":" )[0];
                if (name.startsWith( webName )) {
                    WebApp webApp = new WebApp();
                    webApp.setName( name );
                    if (oldDataStrs[i].split( ":" )[1].equals( "running" )) {
                        if (oldDataStrs[i].split( ":" )[2].equals( "0" )) {
                            webApp.setStatus( "运行" );
                        } else {
                            webApp.setStatus( "异常" );
                        }
                    } else if (oldDataStrs[i].split( ":" )[1].equals( "stopped" )) {
                        if (oldDataStrs[i].split( ":" )[2].equals( "0" )) {
                            webApp.setStatus( "停止" );
                        } else {
                            webApp.setStatus( "异常" );
                        }
                    } else {
                        webApp.setStatus( "异常" );
                    }
                    webAppArrayList.add( webApp );
                }
            }
        }
        return webAppArrayList;
    }

    /**
     * 获取tomcat正在运行的webApp的参数
     *
     * @return
     */
    public static ArrayList<WebApp> getTomcatWebAppDatas(String port, String[] webNames) {

        ArrayList<WebApp> webAppArrayList = new ArrayList<WebApp>();

        String data = TomcatHTMLUtil.message( "http://localhost:" + port + "/manager/text/list" );

        String[] oldDataStrs = data.split( "/" );

        if (oldDataStrs[0].startsWith( "OK" )) {
            for (int i = 0; i < oldDataStrs.length; i++) {
                String name = oldDataStrs[i].split( ":" )[0];
                for (int j = 0; j < webNames.length; j++) {
                    if (name.startsWith( webNames[j] )) {
                        WebApp webApp = new WebApp();
                        webApp.setName( name );
                        if (oldDataStrs[i].split( ":" )[1].equals( "running" )) {
                            if (oldDataStrs[i].split( ":" )[2].equals( "0" )) {
                                webApp.setStatus( "运行" );
                            } else {
                                webApp.setStatus( "异常" );
                            }
                        } else if (oldDataStrs[i].split( ":" )[1].equals( "stopped" )) {
                            if (oldDataStrs[i].split( ":" )[2].equals( "0" )) {
                                webApp.setStatus( "停止" );
                            } else {
                                webApp.setStatus( "异常" );
                            }
                        } else {
                            webApp.setStatus( "异常" );
                        }
                        webAppArrayList.add( webApp );
                    }
                }
            }
        }
        return webAppArrayList;
    }

    /**
     * 获取tomcat正在运行的webApp的参数
     *
     * @return
     */
    public static ArrayList<WebApp> getTomcatWebAppDatasLoading(String port, String[] webNames, Stage stage, Label label) {

        ProgressFrom progressFrom = new ProgressFrom( stage, label );

        ArrayList<WebApp> webAppArrayList = new ArrayList<WebApp>();

        String data = TomcatHTMLUtil.message( "http://localhost:" + port + "/manager/text/list" );

        String[] oldDataStrs = data.split( "/" );

        if (oldDataStrs[0].startsWith( "OK" )) {
            for (int i = 0; i < oldDataStrs.length; i++) {
                String name = oldDataStrs[i].split( ":" )[0];
                for (int j = 0; j < webNames.length; j++) {
                    if (name.startsWith( webNames[j] )) {
                        WebApp webApp = new WebApp();
                        webApp.setName( name );
                        if (oldDataStrs[i].split( ":" )[1].equals( "running" )) {
                            if (oldDataStrs[i].split( ":" )[2].equals( "0" )) {
                                webApp.setStatus( "运行" );
                            } else {
                                webApp.setStatus( "异常" );
                            }
                        } else if (oldDataStrs[i].split( ":" )[1].equals( "stopped" )) {
                            if (oldDataStrs[i].split( ":" )[2].equals( "0" )) {
                                webApp.setStatus( "停止" );
                            } else {
                                webApp.setStatus( "异常" );
                            }
                        } else {
                            webApp.setStatus( "异常" );
                        }
                        webAppArrayList.add( webApp );
                    }
                }
            }
        }
        if (webAppArrayList != null || webAppArrayList.size() > 0) {
            progressFrom.cancelProgressBar();
        }
        return webAppArrayList;
    }

    /**
     * 获取tomcat正在运行的webApp的参数
     *
     * @return
     */
    public static Task<ArrayList<WebApp>> getTomcatWebAppDatasTask(String port, String[] webNames) {

        Task<ArrayList<WebApp>> task = new Task<ArrayList<WebApp>>() {
            @Override
            protected ArrayList<WebApp> call() throws Exception {
                ArrayList<WebApp> webAppArrayList = new ArrayList<WebApp>();
                updateMessage( "web information loadding..." );
                String data = TomcatHTMLUtil.message( "http://localhost:" + port + "/manager/text/list" );

                String[] oldDataStrs = data.split( "/" );

                if (oldDataStrs[0].startsWith( "OK" )) {
                    for (int i = 0; i < oldDataStrs.length; i++) {
                        String name = oldDataStrs[i].split( ":" )[0];
                        for (int j = 0; j < webNames.length; j++) {
                            if (name.startsWith( webNames[j] )) {
                                WebApp webApp = new WebApp();
                                webApp.setName( name );
                                System.out.println( "wenApp loadding : " + webNames[j] );
                                updateMessage( " Loadding " + webNames[j] + "now..." );
                                if (oldDataStrs[i].split( ":" )[1].equals( "running" )) {
                                    if (oldDataStrs[i].split( ":" )[2].equals( "0" )) {
                                        webApp.setStatus( "运行" );
                                    } else {
                                        webApp.setStatus( "异常" );
                                    }
                                } else if (oldDataStrs[i].split( ":" )[1].equals( "stopped" )) {
                                    if (oldDataStrs[i].split( ":" )[2].equals( "0" )) {
                                        webApp.setStatus( "停止" );
                                    } else {
                                        webApp.setStatus( "异常" );
                                    }
                                } else {
                                    webApp.setStatus( "异常" );
                                }
                                webAppArrayList.add( webApp );
                            }
                        }
                    }
                }
                System.out.println( "total web number : " + webAppArrayList.size() );
                updateMessage( "Load webs" + webAppArrayList.toString() + "successfully!" );
                return webAppArrayList;
            }

            @Override
            protected void succeeded() {
                super.succeeded();
                updateMessage( "Done!" );
            }
        };
        return task;

    }

    /**
     * 获取tomcat正在运行的webApp的参数
     *
     * @return
     */
    public Task<ArrayList<WebApp>> getTomcatWebAppDatasTask2(String[] webNames) {

        Task<ArrayList<WebApp>> task = new Task<ArrayList<WebApp>>() {
            @Override
            protected ArrayList<WebApp> call() throws Exception {

                updateMessage( "web information loadding..." );
                String data = TomcatHTMLUtil.message( "http://localhost:" + tomcatPort + "/manager/text/list" );

                String[] oldDataStrs = data.split( "/" );

                if (oldDataStrs[0].startsWith( "OK" )) {
                    for (int i = 0; i < oldDataStrs.length; i++) {
                        String name = oldDataStrs[i].split( ":" )[0];
                        for (int j = 0; j < webNames.length; j++) {
                            if (name.startsWith( webNames[j] )) {
                                WebApp webApp = new WebApp();
                                webApp.setName( name );
                                System.out.println( "wenApp loadding : " + webNames[j] );
                                updateMessage( " Loadding " + webNames[j] + "now..." );
                                Thread.sleep( 1500 );
                                if (oldDataStrs[i].split( ":" )[1].equals( "running" )) {
                                    if (oldDataStrs[i].split( ":" )[2].equals( "0" )) {
                                        webApp.setStatus( "运行" );
                                    } else {
                                        webApp.setStatus( "异常" );
                                    }
                                } else if (oldDataStrs[i].split( ":" )[1].equals( "stopped" )) {
                                    if (oldDataStrs[i].split( ":" )[2].equals( "0" )) {
                                        webApp.setStatus( "停止" );
                                    } else {
                                        webApp.setStatus( "异常" );
                                    }
                                } else {
                                    webApp.setStatus( "异常" );
                                }
                                webAppArrayList.add( webApp );
                            }
                        }
                    }
                }
                System.out.println( "total web number : " + webAppArrayList.size() );
                updateMessage( "Load webs" + webAppArrayList.toString() + "successfully!" );
                Thread.sleep( 1500 );
                return webAppArrayList;
            }

            @Override
            protected void succeeded() {
                super.succeeded();
                InetAddress address = null;
                String hostAddress = "";
                for (WebApp web : webAppArrayList
                        ) {
                    System.out.println( "load webApps successfully : " + web.getName() );
                    System.out.println( "load webApps' status  : " + web.getStatus() );
                    if (webFront.equals( web.getName() ) && "运行".equals( web.getStatus() )) {
                        //update front web project basePath
                                    /*String webFrontWar = GlobalConfInfo.getProjectMap().get( webList.get( i ) ).split( "," )[1]
                                            .split( ":" )[0];
                                    String webFront = webFrontWar.substring( 0, webFrontWar.lastIndexOf( "." ) );*/
                        String path = tomcatpath + "//webapps//" + webFront + "//static//globalParams.json";
                        String pathNew = tomcatpath + "//webapps//" + webFront + "//static//globalParams.json.bak";
                        String basePaths = JsonUtils.readJsonFile( path );
                        JSONObject jsonObject = JSON.parseObject( basePaths );
                        //todo property要统一
                        final String interfecePath = jsonObject.getString( "communicationAddressTestDataComparexcmp" );
                        System.out.println( webList.get( n ) + " project's interfece path ::" + interfecePath );
                        String[] split = interfecePath.split( ":" );
                        StringBuffer buffer = new StringBuffer();
                        for (int f = 0; f < split.length; f++) {
                            System.out.println( split[f] );
                        }
                        int portEndPos = interfecePath.split( ":" )[2].indexOf( "/" );
                        String s = interfecePath.split( ":" )[2].substring( portEndPos );
                        try {
                            address = InetAddress.getLocalHost();
                            hostAddress = address.getHostAddress();
                            System.out.println( "current IPaddress:: " + hostAddress );
                        } catch (UnknownHostException e) {
                            e.printStackTrace();
                        }
                        buffer.append( "http://" + hostAddress + ":" ).append( tomcatPort ).append( s );
                        System.out.println( "the end:" + buffer.toString() );
                        jsonObject.put( "communicationAddressTestDataComparexcmp", buffer.toString() );
                        //wait tooltip
                        if (!updateSuccess) {
                            updateMessage( "当前项目" + webList.get( n ) + "正在配置路由，请耐心等待！" );
                            try {
                                Thread.sleep( 2500 );
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        updateSuccess = JsonUtils.writeJsonFile( jsonObject.toString(), path, pathNew );
                        //update dataBase
                        if (updateSuccess) {
                            if (!reloadWebFront) {
                                updateMessage( "当前项目" + webList.get( n ) + "正在重新加载，请耐心等待！" );
                                try {
                                    Thread.sleep( 2500 );
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                            //reload webFront  耗时等待
                            reloadWebFront = TomcatHTMLUtil.reloadWebApp( webFront, tomcatPort );
                            System.out.println( webList.get( n ) + " front page reload status :: " + reloadWebFront );
                            updateMessage( webList.get( n ) + " front page reload status :: " + reloadWebFront );
                            try {
                                Thread.sleep( 2500 );
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }

                    }
                    if (webData.equals( web.getName() ) && "运行".equals( web.getStatus() )) {
                        //update web database config
                                    /*String webdatatWar = GlobalConfInfo.getProjectMap().get( webList.get( i ) ).split( "," )[1]
                                            .split( ":" )[1];
                                    String webData = webdatatWar.substring( 0, webdatatWar.lastIndexOf( "." ) );*/
                        String pathdata = tomcatpath + "/webapps/" + webData + "/WEB-INF/classes/application.properties";
                        Configuration configuration = new Configuration( pathdata );
                        //String value = "jdbc:mysql://" + text + ":3306/hrms?useUnicode=true&characterEncoding=UTF-8&useSSL=false";
                        String pass = webConfigVo.getPass();
                        String usern = webConfigVo.getUser();
                        String dataBaseIp = webConfigVo.getDataBaseIp();
                        String dataBasePort = webConfigVo.getDataBasePort();
                        String jdbcUrl = "jdbc:mysql://" + dataBaseIp + ":" + dataBasePort +
                                "/data_compare?useUnicode=true&characterEncoding=utf-8";
                        if (!updatePro) {
                            updateMessage( "当前项目" + webList.get( n ) + "正在更新数据源配置，请耐心等待！" );
                            try {
                                Thread.sleep( 2500 );
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        updatePro = configuration.updatePros( pathdata, "spring.datasource.username", usern, "spring.datasource.password", pass );
                        if (updatePro) {
                            boolean reloadWebApp = TomcatHTMLUtil.reloadWebApp( webData, tomcatPort );
                            if (reloadWebApp) {
                                count = 5;
                                updateMessage( "当前项目" + webList.get( n ) + "启动成功 !，请到浏览器访问*****" );
                                try {
                                    Thread.sleep( 2500 );
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        }

                    }
                }
                updateMessage( "Done!" );
                try {
                    Thread.sleep( 1000 );
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        return task;

    }

    /**
     * 获取tomcat正在运行的webApp的参数
     *
     * @return
     */
    public Task<ArrayList<WebApp>> getTomcatWebAppDatasTask21(String[] webNames) {

        Task<ArrayList<WebApp>> task = new Task<ArrayList<WebApp>>() {
            @Override
            protected ArrayList<WebApp> call() throws Exception {

                updateMessage( "web information loadding..." );
                String data = TomcatHTMLUtil.message( "http://localhost:" + tomcatPort + "/manager/text/list" );

                String[] oldDataStrs = data.split( "/" );

                if (oldDataStrs[0].startsWith( "OK" )) {
                    for (int i = 0; i < oldDataStrs.length; i++) {
                        String name = oldDataStrs[i].split( ":" )[0];
                        for (int j = 0; j < webNames.length; j++) {
                            if (name.startsWith( webNames[j] )) {
                                WebApp webApp = new WebApp();
                                webApp.setName( name );
                                System.out.println( "wenApp loadding : " + webNames[j] );
                                updateMessage( " Loadding " + webNames[j] + "now..." );
                                Thread.sleep( 1500 );
                                if (oldDataStrs[i].split( ":" )[1].equals( "running" )) {
                                    if (oldDataStrs[i].split( ":" )[2].equals( "0" )) {
                                        webApp.setStatus( "运行" );
                                    } else {
                                        webApp.setStatus( "异常" );
                                    }
                                } else if (oldDataStrs[i].split( ":" )[1].equals( "stopped" )) {
                                    if (oldDataStrs[i].split( ":" )[2].equals( "0" )) {
                                        webApp.setStatus( "停止" );
                                    } else {
                                        webApp.setStatus( "异常" );
                                    }
                                } else {
                                    webApp.setStatus( "异常" );
                                }
                                webAppArrayList.add( webApp );
                            }
                        }
                    }
                }
                System.out.println( "total web number : " + webAppArrayList.size() );
                updateMessage( "Load webs" + webAppArrayList.toString() + "successfully!" );
                Thread.sleep( 1500 );
                InetAddress address = null;
                String hostAddress = "";
                for (WebApp web : webAppArrayList
                        ) {
                    System.out.println( "load webApps successfully : " + web.getName() );
                    System.out.println( "load webApps' status  : " + web.getStatus() );
                    if (webFront.equals( web.getName() ) && "运行".equals( web.getStatus() )) {
                        //update front web project basePath
                                    /*String webFrontWar = GlobalConfInfo.getProjectMap().get( webList.get( i ) ).split( "," )[1]
                                            .split( ":" )[0];
                                    String webFront = webFrontWar.substring( 0, webFrontWar.lastIndexOf( "." ) );*/
                        String path = tomcatpath + "//webapps//" + webFront + "//static//globalParams.json";
                        String pathNew = tomcatpath + "//webapps//" + webFront + "//static//globalParams.json.bak";
                        String basePaths = JsonUtils.readJsonFile( path );
                        JSONObject jsonObject = JSON.parseObject( basePaths );
                        //todo property要统一
                        final String interfecePath = jsonObject.getString( "communicationAddressTestDataComparexcmp" );
                        System.out.println( webList.get( n ) + " project's interfece path ::" + interfecePath );
                        String[] split = interfecePath.split( ":" );
                        StringBuffer buffer = new StringBuffer();
                        for (int f = 0; f < split.length; f++) {
                            System.out.println( split[f] );
                        }
                        int portEndPos = interfecePath.split( ":" )[2].indexOf( "/" );
                        String s = interfecePath.split( ":" )[2].substring( portEndPos );
                        try {
                            address = InetAddress.getLocalHost();
                            hostAddress = address.getHostAddress();
                            System.out.println( "current IPaddress:: " + hostAddress );
                        } catch (UnknownHostException e) {
                            e.printStackTrace();
                        }
                        buffer.append( "http://" + hostAddress + ":" ).append( tomcatPort ).append( s );
                        System.out.println( "the end:" + buffer.toString() );
                        jsonObject.put( "communicationAddressTestDataComparexcmp", buffer.toString() );
                        //wait tooltip
                        if (!updateSuccess) {
                            updateMessage( "当前项目" + webList.get( n ) + "正在配置路由，请耐心等待！" );
                            try {
                                Thread.sleep( 1500 );
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        updateSuccess = JsonUtils.writeJsonFile( jsonObject.toString(), path, pathNew );
                        //update dataBase
                        if (updateSuccess) {
                            if (!reloadWebFront) {
                                updateMessage( "当前项目" + webList.get( n ) + "正在重新加载，请耐心等待！" );
                                try {
                                    Thread.sleep( 1500 );
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                            //reload webFront  耗时等待
                            reloadWebFront = TomcatHTMLUtil.reloadWebApp( webFront, tomcatPort );
                            System.out.println( webList.get( n ) + " front page reload status :: " + reloadWebFront );
                            updateMessage( webList.get( n ) + " front page reload status :: " + reloadWebFront );
                            try {
                                Thread.sleep( 1500 );
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }

                    }
                    if (webData.equals( web.getName() ) && "运行".equals( web.getStatus() )) {
                        //update web database config
                                    /*String webdatatWar = GlobalConfInfo.getProjectMap().get( webList.get( i ) ).split( "," )[1]
                                            .split( ":" )[1];
                                    String webData = webdatatWar.substring( 0, webdatatWar.lastIndexOf( "." ) );*/
                        String pathdata = tomcatpath + "/webapps/" + webData + "/WEB-INF/classes/application.properties";
                        Configuration configuration = new Configuration( pathdata );
                        //String value = "jdbc:mysql://" + text + ":3306/hrms?useUnicode=true&characterEncoding=UTF-8&useSSL=false";
                        String pass = webConfigVo.getPass();
                        String usern = webConfigVo.getUser();
                        String dataBaseIp = webConfigVo.getDataBaseIp();
                        String dataBasePort = webConfigVo.getDataBasePort();
                        String jdbcUrl = "jdbc:mysql://" + dataBaseIp + ":" + dataBasePort +
                                "/data_compare?useUnicode=true&characterEncoding=utf-8";
                        if (!updatePro) {
                            updateMessage( "当前项目" + webList.get( n ) + "正在更新数据源配置，请耐心等待！" );
                            try {
                                Thread.sleep( 2000 );
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        updatePro = configuration.updatePros( pathdata, "spring.datasource.username", usern, "spring.datasource.password", pass );
                        if (updatePro) {
                            boolean reloadWebApp = TomcatHTMLUtil.reloadWebApp( webData, tomcatPort );
                            if (reloadWebApp) {
                                count = 5;
                                updateMessage( "当前项目" + webList.get( n ) + "启动成功 !，请到浏览器访问*****" );
                                try {
                                    Thread.sleep( 2000 );
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        }

                    }
                }
                return webAppArrayList;
            }

            @Override
            protected void succeeded() {
                super.succeeded();
                updateMessage( "Done!" );
                try {
                    Thread.sleep( 1000 );
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        return task;

    }

    /**
     * 重新部署一个项目
     *
     * @param webAppName
     * @return
     */
    public static boolean reloadWebApp(String webAppName, String port) {
        String data = TomcatHTMLUtil.message( "http://localhost:" + port + "/manager/text/reload?path=/" + webAppName );
        if (data.startsWith( "OK" )) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 停止一个项目
     *
     * @param webAppName
     * @return
     */
    public static boolean stopWebApp(String webAppName, String port) {
        String data = TomcatHTMLUtil.message( "http://localhost:" + port + "/manager/text/stop?path=/" + webAppName );
        if (data.startsWith( "OK" )) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 开始一个项目
     *
     * @param webAppName
     * @return
     */
    public static boolean startWebApp(String webAppName, String port) {
        String data = TomcatHTMLUtil.message( "http://localhost:" + port + "/manager/text/start?path=/" + webAppName );
        if (data.startsWith( "OK" )) {
            return true;
        } else {
            return false;
        }
    }

}
